import Vue from "vue";

Vue.filter('amount', (input) => {
  if (!input && input !== 0) return;
  return '¥' + parseFloat(input).toFixed(2)
})

Vue.filter('capitalize', function (value) {
  if (!value) return ''
  value = value.toString()
  return value.charAt(0).toUpperCase() + value.slice(1)
})
Vue.filter('date', (input) => {
  if (!input) return;
  let datetime = new Date(input * 1000);
  let year = datetime.getFullYear();
  let month = datetime.getMonth() + 1;
  let date = datetime.getDate();
  let hour = datetime.getHours();
  let minute = datetime.getMinutes();
  let second = datetime.getSeconds();
  let mseconds = datetime.getMilliseconds();
  return year + "年" + month + "月" + date + '日';
})
Vue.filter('time', (uData) => {
  if (uData.length === 10) {
    var TimeMyDate = new Date(Number(uData) * 1000)
  } else if (uData.length === 13) {
    TimeMyDate = new Date(Number(uData))
  };
  var myDate = new Date(TimeMyDate);
  let year = myDate.getFullYear();
  let month = myDate.getMonth() + 1;
  let day = myDate.getDate();
  let h = myDate.getHours();
  let m = myDate.getMinutes();
  let s = myDate.getSeconds();
  return year + '年' + month + '月' + day + "日" + " " + fillzero(h) + ':' + fillzero(m) + ':' + fillzero(s);
  //处理补0
  function fillzero(n) {
    return n < 10 ? '0' + n : '' + n;
  }
})
Vue.filter('timeshow', (time_str) => {
  var now = new Date();
  var date = new Date(time_str);
  //计算时间间隔，单位为分钟
  var inter = parseInt((now.getTime() - date.getTime()) / 1000 / 60);
  if (inter == 0) {
    return "刚刚";
  }
  //多少分钟前
  else if (inter < 60) {
    return inter.toString() + "分钟前";
  }
  //多少小时前
  else if (inter < 60 * 24) {
    return parseInt(inter / 60).toString() + "小时前";
  }
  //本年度内，日期不同，取日期+时间  格式如  06-13 22:11
  else if (now.getFullYear() == date.getFullYear()) {
    return (date.getMonth() + 1).toString() + "-" +
      date.getDate().toString() + " " +
      date.getHours() + ":" +
      date.getMinutes();
  } else {
    return date.getFullYear().toString().substring(2, 3) + "-" +
      (date.getMonth() + 1).toString() + "-" +
      date.getDate().toString() + " " +
      date.getHours() + ":" +
      date.getMinutes();
  }
})

Vue.filter('formatTime', (input, type) => {
  if (!input) return;
  let returnVal = '';
  if (type == 1) {
    let tempData = input.split('.');
    returnVal = tempData[0] + '年' + tempData[1] + '月' + tempData[2] + '日'
  }
  return returnVal;
})
Vue.filter('changeNum', (num) => {
  if(num > 10000){
    return Math.floor(num/10000)+"万"
  }else{
    return num
  }
})

Vue.filter('https',(input)=>{
  return input.replace('http://', 'https://');
})

Vue.filter('formatDates',(time, format = 'YY年MM月DD日') =>{
  var date = new Date(time);

  var year = date.getFullYear(),
    month = date.getMonth() + 1, //月份是从0开始的
    day = date.getDate();
  
  var preArr = Array.apply(null, Array(10)).map(function (elem, index) {
    return '0' + index;
  }); ////开个长度为10的数组 格式为 00 01 02 03

  var newTime = format.replace(/YY/g, year)
    .replace(/MM/g, preArr[month] || month)
    .replace(/DD/g, preArr[day] || day);
  return newTime;
})
function formatDate(time, format = 'YY-MM-DD hh:mm:ss'){
  var date = new Date(time);

  var year = date.getFullYear(),
    month = date.getMonth() + 1, //月份是从0开始的
    day = date.getDate(),
    hour = date.getHours(),
    min = date.getMinutes(),
    sec = date.getSeconds();
  var preArr = Array.apply(null, Array(10)).map(function (elem, index) {
    return '0' + index;
  }); ////开个长度为10的数组 格式为 00 01 02 03

  var newTime = format.replace(/YY/g, year)
    .replace(/MM/g, preArr[month] || month)
    .replace(/DD/g, preArr[day] || day)
    .replace(/hh/g, preArr[hour] || hour)
    .replace(/mm/g, preArr[min] || min)
    .replace(/ss/g, preArr[sec] || sec);

  return newTime;

}
/**
 * 
 * 一分钟以内显示 刚刚
 * 一小时以内一分钟以外显示XX分钟前
 * 一天以内一小时以外显示XX小时前
 * 一年以内一天以外显示 MM-DD HH:MM
 * 一年以外显示  YYYY-MM-DD HH:MM
 */
Vue.filter('formatCreateTime', (time) => {
  var date = new Date(time);
  var currentDate = new Date();
  var mills = date.getTime();
  var currentMills = currentDate.getTime();
  var distance = currentMills - mills;
  var finalStr = '';
  if (distance <= 1000 * 60) {
    finalStr = "刚刚";
  } else if (distance <= 1000 * 60 * 60) {
    finalStr = (distance / 60 / 1000).toFixed(0) + "分钟前";
  } else if (distance <= 1000 * 60 * 60 * 24) {
    finalStr = (distance / 1000 / 60 / 60).toFixed(0) + "小时前";
  } else if (distance <= 1000 * 60 * 60 * 24 * 365) {
    // finalStr = (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes();
    finalStr = formatDate(time, 'MM-DD hh:mm');
  } else {
    // finalStr = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes();
    finalStr = formatDate(time, 'YYYY-MM-DD hh:mm');
  }
  return finalStr;
})
